/*
 *	Copyright (C) 2008 - Mauricio Bieze Stefani
 *	This file is part of the Insano Game Engine.
 *
 *	Insano is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Insano is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Insano.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <irrlicht.h>
#include <iostream>
#include "ClientConfig.h"
#include "MBSTrace.h"
#include "MapManager.h"

using namespace std;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

int main()
{
    ClientConfig config;

    config.load("client.xml");

    IrrlichtDevice *device = createDevice( 	config.video.driver,
                                            dimension2d<u32>(config.video.horizontal,config.video.vertical),
                                            config.video.bpp,
                                            config.video.fullscreen,
                                            false,	//stencilbuffer (shadows)
                                            false);	//vsync

    if (device==NULL)
    {
        TRACE_ERROR("NULL device");
        return -1;
    }

    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();

	MapManager mapManager;
	mapManager.setIrrlichtDevice(device);
	if(mapManager.initMapLoader("maps.xml"))
	{
        TRACE_ERROR("loading maps.xml definition file");
		return -1;
	}

	Map *world=mapManager.loadMap(smgr, "world");

	SKeyMap keyMap[] =
	{
		{ EKA_MOVE_FORWARD, KEY_UP } ,		{ EKA_MOVE_FORWARD, KEY_KEY_W },
		{ EKA_MOVE_BACKWARD, KEY_DOWN },	{ EKA_MOVE_BACKWARD, KEY_KEY_S },
		{ EKA_STRAFE_LEFT, KEY_LEFT },		{ EKA_STRAFE_LEFT, KEY_KEY_A },
		{ EKA_STRAFE_RIGHT, KEY_RIGHT },	{ EKA_STRAFE_RIGHT, KEY_KEY_D }
	};

    scene::ISceneNode* camera=smgr->addCameraSceneNodeFPS(0, 100, 500, -1, keyMap, 8, false, 100);
//  smgr->addCameraSceneNodeFPS();
//  smgr->addCameraSceneNodeMaya();

	// if available, use any camera (first found) in the map as start position
	if(ISceneNode *cameraNode=world->getCamera())
	{
		camera->setPosition(cameraNode->getPosition());
		TRACE_INFO("Using map defined camera");
	}

    device->getCursorControl()->setVisible(false);
    int lastFPS = -1;

    while (device->run())
        if (device->isWindowActive())
        {
            driver->beginScene(true, true, video::SColor(0,0,0,0));
            smgr->drawAll();
            driver->endScene();

            int fps = driver->getFPS();

			vector3df pos=camera->getPosition();

            if (lastFPS != fps)
            {
                core::stringw str = L"Map Viewer [";
                str += driver->getName();
                str += "] FPS:";
                str += fps;
                str += " POS(";
                str += pos.X;
                str += ",";
                str += pos.Y;
                str += ",";
                str += pos.Z;
                str += ")";

                device->setWindowCaption(str.c_str());
                lastFPS = fps;
            }
        }

    device->drop();
    return 0;
}

